﻿/*
 * 
 *   订单信息
 *    浇筑时间  年月日 至 年月日
 *    产品编号
 *    泵送数量   立方米 或 吨  
 *    泵送次数
 *    金额
 *    备注信息：  RemarkParameter1方以内泵车使用费RemarkParameter2元  RemarkParameter3方以上泵车使用费每方泵费加收RemarkParameter4元。如需对方第二次移动泵车每次收费RemarkParameter5元，超出RemarkParameter6公里以外混泥土运输，每公里RemarkParameter7元方。
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class Pumper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDDL();
                BindGridView();
            }
        }

        #region Private Method


        private void BindDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDPumperUseType"].ToString()));
        }

        private void BindEditDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlEditStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDPumperUseType"].ToString()));
        }


        private void BindGridView()
        {
            PumperService pumperServiceObj = ServiceFactory.getPumperService();
            OperationResult<List<tb_Pumper>> result = pumperServiceObj.GetAllPumper();

            if (result.operationSuccess)
            {
                gvPumperList.DataSource = result.obj;
                gvPumperList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            BindEditDDL();
            PumperService pumperServiceObj = ServiceFactory.getPumperService();

            OperationResult<tb_Pumper> resultEdit = pumperServiceObj.GetPumperById(id);
            if (resultEdit.operationSuccess == true)
            {
                txtEditSalesOrderSN.Text = resultEdit.obj.SalesOrderId.ToString();
                txtEditStartDate.Text = resultEdit.obj.StartDate.ToString();
                txtEditEndDate.Text = resultEdit.obj.EndDate.ToString();
                txtEditPumpCount.Text = resultEdit.obj.PumpCount.ToString();
                ddlEditCountType.SelectedValue = resultEdit.obj.PumpCountType.ToString();
                txtEditPumpRound.Text = resultEdit.obj.PumpRound.ToString();
                txtEditAmount.Text = resultEdit.obj.Amount.ToString();
                txtEditRemarkParameter1.Text = resultEdit.obj.RemarkParameter1.ToString();
                txtEditRemarkParameter2.Text = resultEdit.obj.RemarkParameter2.ToString();
                txtEditRemarkParameter3.Text = resultEdit.obj.RemarkParameter3.ToString();
                txtEditRemarkParameter4.Text = resultEdit.obj.RemarkParameter4.ToString();
                txtEditRemarkParameter5.Text = resultEdit.obj.RemarkParameter5.ToString();
                txtEditRemarkParameter6.Text = resultEdit.obj.RemarkParameter6.ToString();
                txtEditRemarkParameter7.Text = resultEdit.obj.RemarkParameter7.ToString();
                ddlEditStatus.SelectedValue = resultEdit.obj.StatusId.ToString(); 


                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        }

        public string GetCustomerNameBySalesOrderId(string strSalesOrderId)
        {
            string returnValue = "";

            int intSalesOrderId = Int32.Parse(strSalesOrderId);
            SalesOrderService salesOrderServiceObj = ServiceFactory.getSalesOrderServices();
            OperationResult<tb_SalesOrder> result = salesOrderServiceObj.GetSalesOrderById(intSalesOrderId);
            tb_SalesOrder entity = new tb_SalesOrder();
            if (result.operationSuccess)
            {
                entity = result.obj;
            }
            returnValue = entity.tb_Customer.Name;
            return returnValue;
        }


        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            PumperService pumperServiceObj = ServiceFactory.getPumperService();
            tb_Pumper instance = new tb_Pumper();

            instance.SalesOrderId = Int32.Parse(txtSalesOrderSN.Text);
            instance.StartDate = DateTime.Parse(txtStartDate.Text);
            instance.EndDate = DateTime.Parse(txtEndDate.Text);
            instance.ProductId = 7;  // HardCode
            instance.PumpCount = decimal.Parse(txtPumpCount.Text);
            instance.PumpCountType = Int32.Parse(ddlCountType.SelectedValue);
            instance.PumpRound = Int32.Parse(txtPumpRound.Text);
            instance.Amount = decimal.Parse(txtAmount.Text);
            instance.RemarkParameter1 = txtRemarkParameter1.Text;
            instance.RemarkParameter2 = txtRemarkParameter2.Text;
            instance.RemarkParameter3 = txtRemarkParameter3.Text;
            instance.RemarkParameter4 = txtRemarkParameter4.Text;
            instance.RemarkParameter5 = txtRemarkParameter5.Text;
            instance.RemarkParameter6 = txtRemarkParameter6.Text;
            instance.RemarkParameter7 = txtRemarkParameter7.Text;
            instance.StatusId = Int32.Parse(ddlStatus.SelectedValue);

            OperationResult<tb_Pumper> result = pumperServiceObj.CreatePumper(instance);

            BindGridView();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            PumperService PumperServiceObj = ServiceFactory.getPumperService();
            tb_Pumper entity = new tb_Pumper();

            entity.Id = editId;
            entity.SalesOrderId = Int32.Parse(txtEditSalesOrderSN.Text);
            entity.StartDate = DateTime.Parse(txtEditStartDate.Text);
            entity.EndDate = DateTime.Parse(txtEditEndDate.Text);
            entity.ProductId = 7;  // HardCode
            entity.PumpCount = decimal.Parse(txtEditPumpCount.Text);
            entity.PumpCountType = Int32.Parse(ddlEditCountType.SelectedValue);
            entity.PumpRound = Int32.Parse(txtEditPumpRound.Text);
            entity.Amount = decimal.Parse(txtEditAmount.Text);
            entity.RemarkParameter1 = txtEditRemarkParameter1.Text;
            entity.RemarkParameter2 = txtEditRemarkParameter2.Text;
            entity.RemarkParameter3 = txtEditRemarkParameter3.Text;
            entity.RemarkParameter4 = txtEditRemarkParameter4.Text;
            entity.RemarkParameter5 = txtEditRemarkParameter5.Text;
            entity.RemarkParameter6 = txtEditRemarkParameter6.Text;
            entity.RemarkParameter7 = txtEditRemarkParameter7.Text;
            entity.StatusId = Int32.Parse(ddlEditStatus.SelectedValue);
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_Pumper> resultEdit = PumperServiceObj.ModifyPumper(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            PumperService puperServiceObj = ServiceFactory.getPumperService();
            tb_Pumper deleteEntity = new tb_Pumper();
            deleteEntity.Id = deleteId;
            puperServiceObj.DeletePumper(deleteEntity);
            BindGridView();
        }

        protected void gvPumperList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvPumperList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPumperList.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        #endregion
    }
}
